//
//  MainTabBar.swift
//  lixiangweibo
//
//  Created by dyy on 15/11/24.
//  Copyright © 2015年 lixianggongzuoshi. All rights reserved.
//

import UIKit

class MainTabBar: UITabBar {
    
    override func layoutSubviews() {
        super.layoutSubviews()
        //按钮的总数
        let btnCount = 5
        //按钮的宽度
        let w = self.bounds.size.width / CGFloat(btnCount)
        //按钮的高度
        let h = self.bounds.size.height
        //按钮的固定大小
        let frame = CGRectMake(0, 0, w, h)
        //设置按钮索引并初始化
        var index = 0
        //遍历子控件键
        for v in subviews{
        
            if v is UIControl && !(v is UIButton){
            //设置item的frame
            v.frame = CGRectOffset(frame, CGFloat(index) * w, 0)
            //三目运算确定每个item的位置
                index += (index == 1) ? 2 : 1
            
                //print(v.frame)
            }
            
            
        }
        self.comparButton.frame = CGRectOffset(frame, 2 * w, 0);
        //指定撰写按钮的位置
//        self.comparButton.frame = frame;
//        
//       self.comparButton.center = CGPointMake(self.center.x, h * 0.5)
        
        
        
        
    }
    //懒加载撰写按钮
    lazy var comparButton: UIButton = {
    let btn = UIButton()
    //设置默认状态和选中状态图片
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState:UIControlState.Normal)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState:UIControlState.Selected)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Selected)
        
       // btn.backgroundColor = UIColor.redColor()
        //添加按钮
        self.addSubview(btn)
        
        
    return btn
    }()
    
    
}
